package com.global.gbinventory.data.tablemodel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.barang.util.DBMaster;
import com.global.gbinventory.barang.util.TYPE;
import com.global.gbinventory.data.model.Supplier;
import com.global.gbinventory.main.model.GBTableModel;

public class SupplierTableModel extends GBTableModel{

	private static final long serialVersionUID = -8028091727173676247L;
	
	private List<Supplier> supplierList; //list to save all supplier according to DB
	private DBMaster dbMaster;
	
	private TYPE type;
	
	public SupplierTableModel(DBMaster dbMaster){
		super();
		this.dbMaster = dbMaster;
		supplierList = new ArrayList<Supplier>();
		type = TYPE.SUPPLIERs;
		initialize();
	}
	
	/**
	 * initialize the needed data
	 */
	private void initialize(){
		initTableData();
	}
	
	/**
	 * initialize the table model using the data from DB
	 * Furthermore, it also initialize the supplierList
	 */
	private void initTableData(){
		List<Object[]> dbTable = dbMaster.getData(type);
		List<Object[]> tmData = new ArrayList<Object[]>();
		Object[] tmCols = null;
		
		for(Object[] obj : dbTable){
			//add data from DB to supplierList
			int id = (Integer) obj[CONSTANT.SUPPLIER_DB_ID];
			String kode = obj[CONSTANT.SUPPLIER_DB_KODE].toString();
			String nama = obj[CONSTANT.SUPPLIER_DB_NAMA].toString();
			String alamat = obj[CONSTANT.SUPPLIER_DB_ALAMAT].toString();
			String kota = obj[CONSTANT.SUPPLIER_DB_KOTA].toString();
			String kodepos = obj[CONSTANT.SUPPLIER_DB_KODEPOS].toString();
			String provinsi = obj[CONSTANT.SUPPLIER_DB_PROVINSI].toString();
			String telepon = obj[CONSTANT.SUPPLIER_DB_TELEPON].toString();
			String namaagen = obj[CONSTANT.SUPPLIER_DB_NAMAAGEN].toString();
			String hpagen = obj[CONSTANT.SUPPLIER_DB_HPAGEN].toString();
			String emailagen = obj[CONSTANT.SUPPLIER_DB_EMAILAGEN].toString();
			String keterangan = obj[CONSTANT.SUPPLIER_DB_KETERANGAN].toString();
			Supplier supplier = new Supplier(id, kode, nama, alamat, kota, 
					kodepos, provinsi, telepon, namaagen, hpagen, emailagen, keterangan);
			supplierList.add(supplier);
			
			//add data from DB to table model
			tmCols = new Object[type.getColumnCount()];
			tmCols[CONSTANT.SUPPLIER_TM_KODE] = kode;
			tmCols[CONSTANT.SUPPLIER_TM_NAMA] = nama;
			tmCols[CONSTANT.SUPPLIER_TM_ALAMAT] = alamat;
			tmCols[CONSTANT.SUPPLIER_TM_KOTA] = kota;
			tmCols[CONSTANT.SUPPLIER_TM_KODEPOS] = kodepos;
			tmCols[CONSTANT.SUPPLIER_TM_PROVINSI] = provinsi;
			tmCols[CONSTANT.SUPPLIER_TM_TELEPON] = telepon;
			tmCols[CONSTANT.SUPPLIER_TM_NAMAAGEN] = namaagen;
			tmCols[CONSTANT.SUPPLIER_TM_HPAGEN] = hpagen;
			tmCols[CONSTANT.SUPPLIER_TM_EMAILAGEN] = emailagen;
			tmCols[CONSTANT.SUPPLIER_TM_KETERANGAN] = keterangan;
			tmData.add(tmCols);
		}
		
		setColumnNames(type.getColumnNames());
		setData(tmData);
		
	}
	
	public Supplier getSupplier(int index){
		return supplierList.get(index);
	}
	
	private void addSupplier(String kode, String nama, String alamat, String kota, String kodepos, 
			String provinsi, String telepon, String namaagen, String hpagen, 
			String emailagen, String keterangan){
		
		Object[] tuple = new Object[]{kode, nama, alamat, kota, kodepos, provinsi, telepon, 
				namaagen, hpagen, emailagen, keterangan};
		try {
			dbMaster.addNewTuple(type, tuple);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		addRow(tuple);
		
		Supplier supplier = new Supplier(getNewId(), kode, nama, alamat, kota, kodepos, 
				provinsi, telepon, namaagen, hpagen, emailagen, keterangan);
		supplierList.add(supplier);
		
	}
	
	private void update(int index, String kode, String nama, String alamat, String kota, String kodepos, 
			String provinsi, String telepon, String namaagen, String hpagen, 
			String emailagen, String keterangan){
		
		Object[] tuple = new Object[]{kode, nama, alamat, kota, kodepos, provinsi, telepon, 
				namaagen, hpagen, emailagen, keterangan};
		
		Supplier supplier = supplierList.get(index);
		try {
			int id = supplier.getId();
			dbMaster.updateTuple(type, id, tuple);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		setTuple(index, tuple);
		supplier.updateSupplier(kode, nama, alamat, kota, kodepos, provinsi, telepon, namaagen, 
				hpagen, emailagen, keterangan);
		
	}
	
	public void updateSupplier(int index, String kode, String nama, String alamat, String kota, 
			String kodepos, String provinsi, String telepon, String namaagen, String hpagen, 
			String emailagen, String keterangan){
		
		if(index < 0){
			addSupplier(kode, nama, alamat, kota, kodepos, provinsi, 
					telepon, namaagen, hpagen, emailagen, keterangan);
			
		}else{
			update(index, kode, nama, alamat, kota, kodepos, provinsi, telepon, 
					namaagen, hpagen, emailagen, keterangan);
		}
		
	}
	
	public void removeSupplier(int index){
		Supplier supplier = supplierList.get(index);
		try {
			int id = supplier.getId();
			dbMaster.removeTuple(type, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		supplierList.remove(index);
		removeRow(index);
		
	}
	
	public boolean isEmptyTable(){
		return supplierList.isEmpty();
	}
	
	@Override
    public boolean isCellEditable(int row, int col) {
    	return false;
    }
    
	public String isExistedKode(String kode){
		for(Supplier s : supplierList){
			if(s.getKode().equalsIgnoreCase(kode)){
				return kode;
			}
		}
		return CONSTANT.EMPTY_STRING;
	}
	
	/**
	 * return a new primary key (id) corresponding to 
	 * the autoincrement id in DB
	 * @return
	 */
	private int getNewId(){
		if(!(supplierList.isEmpty())){
			Supplier lastSupplier = supplierList.get(supplierList.size() - 1);
			int lastId = lastSupplier.getId();
			return lastId + 1;
		}else{
			return 1;
		}
		
	}
}
